﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Newtonsoft.Json;
namespace BallData
{
    /// <summary>
    /// Interaction logic for Match_Filters.xaml
    /// </summary>
    public partial class Match_Filters : Window
    {
        private MainUIControl uicontrol;

        public MainUIControl Uicontrol
        {
            get { return uicontrol; }
            set { uicontrol = value; }
        }

        public Match_Filters()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
            String config = ConfigHelper.GetConfig("Match_Filters_FilterList","");

            if (config.Length < 3)
            {
                Match_FilterItemBind filter_oddsbind = new Match_FilterItemBind { szTitle = "盘口", FilterTypes = OddsUntil.filter_oddstypes, szSelectFilterType = "让球", szFilter = "半球", bFilter = false };

                Match_FilterItemBind filter_leaguedatabind = new Match_FilterItemBind { szTitle = "联赛", FilterTypes = OddsUntil.filter_leaguetypes, szSelectFilterType = "得分", szFilter = "3", bFilter = false };

                Match_FilterItemBind filter_history_vsbind = new Match_FilterItemBind { szTitle = "对阵", FilterTypes = OddsUntil.filter_history_vss, szSelectFilterType = "胜", szFilter = "3", bFilter = false };

                Match_FilterItemBind filter_close_vsbind = new Match_FilterItemBind { szTitle = "交战", FilterTypes = OddsUntil.filter_close_vss, szSelectFilterType = "连胜", szFilter = "4", bFilter = false };


                OddsUntil.FilterList.Add(filter_oddsbind);
                OddsUntil.FilterList.Add(filter_leaguedatabind);
                OddsUntil.FilterList.Add(filter_history_vsbind);
                OddsUntil.FilterList.Add(filter_close_vsbind);


                Match_FilterItem filter_odds = new Match_FilterItem();
                filter_odds.Uicontrol = Uicontrol;
                filter_odds.Filterbind = filter_oddsbind;

                this.stackPanel1.Children.Add(filter_odds);

                Match_FilterItem filter_leaguedata = new Match_FilterItem();
                filter_leaguedata.Uicontrol = Uicontrol;
                filter_leaguedata.Filterbind = filter_leaguedatabind;

                this.stackPanel1.Children.Add(filter_leaguedata);

                Match_FilterItem filter_history_vs = new Match_FilterItem();
                filter_history_vs.Uicontrol = Uicontrol;
                filter_history_vs.Filterbind = filter_history_vsbind;

                this.stackPanel1.Children.Add(filter_history_vs);

                Match_FilterItem filter_close_vs = new Match_FilterItem();
                filter_close_vs.Uicontrol = Uicontrol;
                filter_close_vs.Filterbind = filter_close_vsbind;

                this.stackPanel1.Children.Add(filter_close_vs);

                Button addbutton = new Button();

                addbutton.Content = "添加过滤";
                addbutton.Width = filter_close_vs.Width;
                addbutton.Height = filter_close_vs.Height;
                addbutton.Margin = filter_close_vs.Margin;
                addbutton.Click += new RoutedEventHandler(addbutton_Click);
                this.stackPanel1.Children.Add(addbutton);

                ContextMenu filtermenu = new ContextMenu();
                MenuItem mfilter_odds = new MenuItem();
                mfilter_odds.Header = "盘口";
                mfilter_odds.Click += new RoutedEventHandler(filter_odds_Click);
                filtermenu.Items.Add(mfilter_odds);

                MenuItem mfilter_leaguedata = new MenuItem();
                mfilter_leaguedata.Header = "联赛";
                mfilter_leaguedata.Click += new RoutedEventHandler(filter_leaguedata_Click);
                filtermenu.Items.Add(mfilter_leaguedata);

                MenuItem mfilter_history = new MenuItem();
                mfilter_history.Header = "对阵";
                mfilter_history.Click += new RoutedEventHandler(filter_history_Click);
                filtermenu.Items.Add(mfilter_history);

                MenuItem mfilter_close_vs = new MenuItem();
                mfilter_close_vs.Header = "交战";
                mfilter_close_vs.Click += new RoutedEventHandler(filter_close_vs_Click);
                filtermenu.Items.Add(mfilter_close_vs);

                addbutton.ContextMenu = filtermenu;

            }
            else
            {
                OddsUntil.FilterList = JsonConvert.DeserializeObject<List<Match_FilterItemBind>>(config);

                Match_FilterItem filter_odds = null;

                foreach (Match_FilterItemBind mi in OddsUntil.FilterList)
                {

                    filter_odds = new Match_FilterItem();
                    filter_odds.Uicontrol = Uicontrol;
                    filter_odds.Filterbind = mi;

                    this.stackPanel1.Children.Add(filter_odds);

                }

                Button addbutton = new Button();

                addbutton.Content = "添加过滤";
                addbutton.Width = filter_odds.Width;
                addbutton.Height = filter_odds.Height;
                addbutton.Margin = filter_odds.Margin;
                addbutton.Click += new RoutedEventHandler(addbutton_Click);
                this.stackPanel1.Children.Add(addbutton);

                ContextMenu filtermenu = new ContextMenu();
                MenuItem mfilter_odds = new MenuItem();
                mfilter_odds.Header = "盘口";
                mfilter_odds.Click += new RoutedEventHandler(filter_odds_Click);
                filtermenu.Items.Add(mfilter_odds);

                MenuItem mfilter_leaguedata = new MenuItem();
                mfilter_leaguedata.Header = "联赛";
                mfilter_leaguedata.Click += new RoutedEventHandler(filter_leaguedata_Click);
                filtermenu.Items.Add(mfilter_leaguedata);

                MenuItem mfilter_history = new MenuItem();
                mfilter_history.Header = "对阵";
                mfilter_history.Click += new RoutedEventHandler(filter_history_Click);
                filtermenu.Items.Add(mfilter_history);

                MenuItem mfilter_close_vs = new MenuItem();
                mfilter_close_vs.Header = "交战";
                mfilter_close_vs.Click += new RoutedEventHandler(filter_close_vs_Click);
                filtermenu.Items.Add(mfilter_close_vs);

                addbutton.ContextMenu = filtermenu;
            }
            

            

            
        }

        void addbutton_Click(object sender, RoutedEventArgs e)
        {
            (sender as Button).ContextMenu.IsOpen = true;
        }

        void filter_close_vs_Click(object sender, RoutedEventArgs e)
        {
            Match_FilterItemBind filter_close_vsbind = new Match_FilterItemBind { szTitle = "交战", FilterTypes = OddsUntil.filter_close_vss, szFilter = "4", bFilter = false };
            OddsUntil.FilterList.Add(filter_close_vsbind);
            Match_FilterItem filter_close_vs = new Match_FilterItem();
            filter_close_vs.Uicontrol = Uicontrol;
            filter_close_vs.Filterbind = filter_close_vsbind;
            this.stackPanel1.Children.Insert(this.stackPanel1.Children.Count - 1, filter_close_vs);
            save();
            
        }

        void filter_history_Click(object sender, RoutedEventArgs e)
        {
            Match_FilterItemBind filter_history_vsbind = new Match_FilterItemBind { szTitle = "对阵", FilterTypes = OddsUntil.filter_history_vss, szFilter = "3", bFilter = false };
            OddsUntil.FilterList.Add(filter_history_vsbind);
            Match_FilterItem filter_history_vs = new Match_FilterItem();
            filter_history_vs.Uicontrol = Uicontrol;
            filter_history_vs.Filterbind = filter_history_vsbind;
            this.stackPanel1.Children.Insert(this.stackPanel1.Children.Count - 1, filter_history_vs);
            save();
            
        }

        void filter_leaguedata_Click(object sender, RoutedEventArgs e)
        {
            Match_FilterItemBind filter_leaguedatabind = new Match_FilterItemBind { szTitle = "联赛", FilterTypes = OddsUntil.filter_leaguetypes, bFilter = false };
            OddsUntil.FilterList.Add(filter_leaguedatabind);
            Match_FilterItem filter_leaguedata = new Match_FilterItem();
            filter_leaguedata.Uicontrol = Uicontrol;
            filter_leaguedata.Filterbind = filter_leaguedatabind;
            this.stackPanel1.Children.Insert(this.stackPanel1.Children.Count - 1, filter_leaguedata);
            save();
            
        }

        void filter_odds_Click(object sender, RoutedEventArgs e)
        {

            Match_FilterItemBind filter_oddsbind = new Match_FilterItemBind { szTitle = "盘口", FilterTypes = OddsUntil.filter_oddstypes, szFilter = "半球", bFilter = false };
            OddsUntil.FilterList.Add(filter_oddsbind);
            Match_FilterItem filter_odds = new Match_FilterItem();
            filter_odds.Uicontrol = Uicontrol;
            filter_odds.Filterbind = filter_oddsbind;
            this.stackPanel1.Children.Insert(this.stackPanel1.Children.Count - 1,filter_odds);
            save();
            
        }
        void save()
        {
            String json = JsonConvert.SerializeObject(OddsUntil.FilterList);
            ConfigHelper.SetConfig("Match_Filters_FilterList", json);
        }


        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            
            this.Hide();
            save();

            e.Cancel = true;
        }
    }
}
